<?php

class boundController extends baseController {
	
	/** 盘点库存 */
	public function inventory(){
		$where = array();
		if($sno=fetch_a_string('sno')){
			$where[] = "sno LIKE '{$sno}%'";
		}
		if($key=fetch_a_string('key')){
			$fields = array('sno','name','specific','unit','brand','remark');
			$GLOBALS['S_KEY'] = $key;
			$func = function($v){ return "`{$v}` LIKE '%{$GLOBALS['S_KEY']}%'"; };
			$where[] = ('('. join(' OR ', array_map($func, $fields)). ')');
		}
		$where = join(' AND ', $where);
		$count = Ware::count($where);
		$limit = PagerHelper::split2limit($count);
		$list = Ware::getList($where, $limit[0], $limit[1]);
		$this->assign('list', $list);
                
		$this->display();
	}
	
	/** 库存汇总 */
	public function summary(){
		$from = intval(str_replace('-','',fetch_a_string('from')));
		$to   = intval(str_replace('-','',fetch_a_string('to')));
		
		$list = BoundHelper::getItemsSummaryList($from, $to);
                
		$this->assign('list', $list);
                $this->assign('counts', count($list));
		$this->display();
	}
	
	/** 库存明细 */
	public function detail(){
		$where = "effect=1";
		
		$from = intval(str_replace('-','',fetch_a_string('from')));
		$to   = intval(str_replace('-','',fetch_a_string('to')));
		if($from || $to){
			if(!$from) $from = '20110101';
			if(!$to) $to = date('Ymd');
			$where .= " AND sum_day >= {$from} AND sum_day <= {$to}";
		}
		
		$limit = PagerHelper::split2limit(BoundHelper::getItemsCount($where));
		$list = BoundHelper::getItemsList($where, 'bound_id ASC, sno ASC', $limit[0], $limit[1] );
		$this->assign('list', $list);
		$this->display();
	}
	
	/** 库存单据 */
	public function views(){
		$where = array();
		if($sno=fetch_a_string('sno')){
			$sno = Db::qstr($sno);
			$where[] = "(sno LIKE '{$sno}%' OR indent_sno LIKE '{$sno}%')";
		}
		$from = fetch_a_string('from') ? fetch_a_string('from').' 00:00:00' : '';
		$to   = fetch_a_string('to') ? fetch_a_string('to').' 23:59:59' : '';
		if($from)	$where[] = 'addtime>='.strtotime($from);
		if($to)		$where[] = 'addtime<='.strtotime($to);
		if($type=fetch_a_int('type')){
			$where[] = "type={$type}";
		}
		if($user_id=fetch_a_int('user_id')){
			$where[] = "user_id={$user_id}";
		}
		$where = join(' AND ', $where);
		
		$count = Db::get('_Bound')->count()->where($where)->getCol();
		$limit = PagerHelper::split2limit($count);
		$list  = Db::get('_Bound')->select()->where($where)->order('id DESC')->limit($limit)->getAll();
		$this->assign('list', $list);
		$this->assign('types', BoundHelper::type2txt());
		$this->display();
	}
	
	/** 查看库存单据的跳转 */
	public function view(){
		$bound_id = fetch_a_int('id');
		if($bound_id && $bound=Bound::get($bound_id)){
			if($bound['class']=='in'){
				$this->redirect("inbound-view/id/{$bound_id}");
			}
			else{
				$this->redirect("outbound-view/id/{$bound_id}");
			}
		}
		Show::warn('无此库存单据');
	}
	
}

?>
